package cn.itcast.util;

import cn.itcast.domain.StringToUrl;
import cn.itcast.domain.Student;
import cn.itcast.domain.Teacher;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class AuthFilter implements Filter {


    public void init(FilterConfig filterConfig) throws ServletException {

    }


    public void doFilter(ServletRequest srequest, ServletResponse sresponse,
                         FilterChain filterChain)
            throws IOException, ServletException {
        // 1 表示存在该路径 0表示没有
        int i = -1;
        String url[] = StringToUrl.url();
        HttpServletRequest request = (HttpServletRequest) srequest;
        HttpServletResponse response = (HttpServletResponse) sresponse;
        String path=request.getRequestURI();
        System.out.println("请求路径"+path);

        HttpSession session=request.getSession();
        Student student = (Student) session.getAttribute("student");
//        String studentss = (String) session.getAttribute("studentss");
        for (String urll : url){
            if (urll.equals(path)){
                i = 1;
                break;
            }else {
                i = 0;
            }
        }

//        System.out.println("i = "+ i);

        if ( i == 1 ){
            filterChain.doFilter(request, response);
            System.out.println("我放行了"+path);
        }else {
            if (student != null || path.equals("end")){
                filterChain.doFilter(request, response);
            }



//        if ( path.equals("/StuAndTea_war/") || path.equals("/StuAndTea_war/login")
//                || path.equals("/StuAndTea_war/index.html")
//                || path.equals("/StuAndTea_war/static/js/jquery-3.1.1.min.js")
//                || path.equals("/StuAndTea_war/static/img/01.png")){
//            System.out.println(1);
//            filterChain.doFilter(request, response);
//            System.out.println(2);
//        }else {
//            if (student != null || path.equals("end")){
//                System.out.println("1"+student);
//                filterChain.doFilter(request, response);
//            }
//
        }


    }

    public void destroy() {

    }

}
